package com.javaweb.web.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.javaweb.base.BaseDao;
import com.javaweb.base.po.Schedule;
import com.javaweb.base.po.User;
import com.javaweb.constant.CommonConstant;
import com.javaweb.util.core.DateUtil;
import com.javaweb.web.eo.schedule.ScheduleAddRequest;
import com.javaweb.web.eo.schedule.ScheduleListRequest;
import com.javaweb.web.eo.schedule.ScheduleListResponse;
import com.javaweb.web.service.ScheduleService;

/**
 * 日程模块业务层接口实现
 * 
 * @author tank2140896
 */
@Service("scheduleServiceImpl")
public class ScheduleServiceImpl extends BaseDao implements ScheduleService {
	
	/**
	 * 根据日期获取日程信息业务层接口实现
	 * 
	 * @param scheduleListRequest 日期信息请求参数
	 * @return List<ScheduleListResponse> 日程信息列表
	 */
	@Override
	public List<ScheduleListResponse> getScheduleByDate(ScheduleListRequest scheduleListRequest){
		List<String> allDates = getAllDates(scheduleListRequest.getYear(),scheduleListRequest.getMonth());
		Map<String,String> map = new HashMap<String,String>();
		map.put("startDate",allDates.get(0));
		map.put("endDate",allDates.get(allDates.size()-1));
		List<Schedule> scheduleByDateList = scheduleDao.getScheduleByDate(map);
		List<ScheduleListResponse> scheduleList = new ArrayList<>();
		for(int i=0;i<allDates.size();i++){
			ScheduleListResponse scheduleListResponse = new ScheduleListResponse();
			String date = allDates.get(i);
			boolean continueFlag = true;
			for(int j=0;j<scheduleByDateList.size();j++){
				String companyDate = scheduleByDateList.get(j).getScheduleDate();
				if(date.equals(companyDate)){
					scheduleListResponse.setScheduleDate(date);
					scheduleListResponse.setScheduleType(scheduleByDateList.get(j).getScheduleType());
					continueFlag = false;
					break;
				}
			}
			if(continueFlag){
				int weekendsFlag = DateUtil.isWeekends(date,DateUtil.DEFAULT_DATE_PATTERN)==true?1:2;//特殊日期处理完后，如果不是1周末的话那就是正常日
				scheduleListResponse.setScheduleDate(date);
				scheduleListResponse.setScheduleType(weekendsFlag);
			}
			scheduleList.add(scheduleListResponse);
		}
		return scheduleList;
	}
	
	/**
	 * 保存日程业务层接口实现
	 * 
	 * @param scheduleAddRequest 日期信息保存请求参数
	 * @return user 用户信息
	 */
	@Transactional
	@Override
	public void scheduleSave(ScheduleAddRequest scheduleAddRequest,User user) {
		List<ScheduleListResponse> list = scheduleAddRequest.getList();
		list = list.stream().sorted(new Comparator<ScheduleListResponse>() {
			public int compare(ScheduleListResponse a, ScheduleListResponse b) {
				return
				Integer.parseInt(a.getScheduleDate().replaceAll(CommonConstant.BAR,CommonConstant.EMPTY_VALUE))
				>
				Integer.parseInt(b.getScheduleDate().replaceAll(CommonConstant.BAR,CommonConstant.EMPTY_VALUE))
				?0:-1;
			}
		}).toList();
		List<Schedule> scheduleList = new ArrayList<>();
		Date date = new Date();
		for(int i=0;i<list.size();i++){
			Schedule schedule = new Schedule();
			schedule.setId(idGenerator.idCreate());
			schedule.setScheduleDate(list.get(i).getScheduleDate());
			schedule.setScheduleType(list.get(i).getScheduleType());
			schedule.setCreator(user.getUserId());
			schedule.setCreateDate(date);
			schedule.setUpdater(user.getUserId());
			schedule.setUpdateDate(new Date());
			schedule.setDelFlag(CommonConstant.ZERO_NUMBER_VALUE);
			scheduleList.add(schedule);
		}
		Map<String,String> map = new HashMap<>();
		map.put("startDate",list.get(0).getScheduleDate());
		map.put("endDate",list.get(list.size()-1).getScheduleDate());
		scheduleDao.deleteByScheduleDate(map);
		scheduleDao.scheduleSave(scheduleList);
	}
	
	/**
	 * 特殊日期处理，获得一共6*7=42天，满足前端界面显示
	 * 
	 * @param year 年
	 * @param month 月
	 * @return List<String> 日期列表
	 */
	private List<String> getAllDates(String year,String month){
		String yearMonth = year + CommonConstant.BAR + month + CommonConstant.BAR;
		String startDate = yearMonth + DateUtil.FIRST_DAY_OF_MONTH_STRING_FORMAT;
		String endDate = yearMonth + DateUtil.getLastDayOfMonth(Integer.parseInt(year),Integer.parseInt(month));
		List<String> allDates = DateUtil.getAllDates(startDate,endDate,DateUtil.DEFAULT_DATE_PATTERN);
		//获得startDate是星期几的计数（星期一记为1，星期二记为2......星期日记为7），这里简记为N，往前推N-1天
		int startCount = DateUtil.getDayOfWeek(startDate,DateUtil.DEFAULT_DATE_PATTERN) - 1;
		List<String> beforeList = DateUtil.getBeforeDays(startDate,DateUtil.DEFAULT_DATE_PATTERN,startCount);
		int rest = 42 - (allDates.size() + beforeList.size());
		List<String> afterList = DateUtil.getAfterDays(endDate,DateUtil.DEFAULT_DATE_PATTERN,rest);
		List<String> finalList = new ArrayList<>();
		finalList.addAll(beforeList);
		finalList.addAll(allDates);
		finalList.addAll(afterList);
		return finalList;
	}
	
}
